import asyncio
from playwright.async_api import async_playwright
import os
from dao.BaseRepository import BaseRepository
from loguru import logger
import pymongo
import re
import time
baseRep = BaseRepository(collectionname="xdgame")
xdgame_storage_state = "xdgame_storage_state.json"
async def main():
    async with async_playwright() as p:        
        browser = await p.chromium.launch(headless=False,slow_mo=100)
        if os.path.exists(xdgame_storage_state):
            context = await browser.new_context(storage_state=xdgame_storage_state)
        else:
            context = await browser.new_context()
        # create a new page inside context.
        page = await context.new_page()
        await page.goto("https://www.xdgame.com/")
        # dispose context once it is no longer needed.
        # results, nums, _ = baseRep.get_all_no_page(sort=["_id",pymongo.DESCENDING])
        # print(nums)
        # print(results[:10])
        input("按任意键继续")
        await context.storage_state(path=xdgame_storage_state)
        
        input("按任意键继续")   
        await context.close()
       
 


asyncio.run(main())
